package com.stark.commons.spring.web.boot.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.stark.commons.spring.web.support.exception.BindExceptionHandler;
import com.stark.commons.spring.web.support.exception.ConstraintViolationExceptionHandler;
import com.stark.commons.spring.web.support.exception.HttpMessageConversionExceptionHandler;
import com.stark.commons.spring.web.support.exception.HystrixBadRequestExceptionHandler;
import com.stark.commons.spring.web.support.exception.InvalidGrantExceptionHandler;
import com.stark.commons.spring.web.support.exception.KnownExceptionHandler;
import com.stark.commons.spring.web.support.exception.MethodArgumentNotValidExceptionHandler;
import com.stark.commons.spring.web.support.exception.MethodArgumentTypeMismatchExceptionHandler;
import com.stark.commons.spring.web.support.exception.ResourceNotFoundExceptionHandler;
import com.stark.commons.spring.web.support.exception.UnauthorizedExceptionHandler;

/**
 * Web Mvc 自动化配置。
 * @author Ben
 * @since 1.0.0
 * @version 1.0.0
 */
@Configuration
public class ExceptionHandlerAutoConfig {
	
	@Bean
	@ConditionalOnMissingBean(name = "bindExceptionHandler")
	public BindExceptionHandler bindExceptionHandler() {
		return new BindExceptionHandler();
	}
	
	@Bean
	@ConditionalOnClass(name = "javax.validation.ConstraintViolationException")
	@ConditionalOnMissingBean(name = "constraintViolationExceptionHandler")
	public ConstraintViolationExceptionHandler constraintViolationExceptionHandler() {
		return new ConstraintViolationExceptionHandler();
	}
	
	@Bean
	@ConditionalOnMissingBean(name = "httpMessageConversionExceptionHandler")
	public HttpMessageConversionExceptionHandler httpMessageConversionExceptionHandler() {
		return new HttpMessageConversionExceptionHandler();
	}
	
	@Bean
	@ConditionalOnClass(name = "com.netflix.hystrix.exception.HystrixBadRequestException")
	@ConditionalOnMissingBean(name = "hystrixBadRequestExceptionHandler")
	public HystrixBadRequestExceptionHandler hystrixBadRequestExceptionHandler() {
		return new HystrixBadRequestExceptionHandler();
	}
	
	@Bean
	@ConditionalOnClass(name = "org.springframework.security.oauth2.common.exceptions.InvalidGrantException")
	@ConditionalOnMissingBean(name = "invalidGrantExceptionHandler")
	public InvalidGrantExceptionHandler invalidGrantExceptionHandler() {
		return new InvalidGrantExceptionHandler();
	}
	
	@Bean
	@ConditionalOnMissingBean(name = "knownExceptionHandler")
	public KnownExceptionHandler knownExceptionHandler() {
		return new KnownExceptionHandler();
	}
	
	@Bean
	@ConditionalOnMissingBean(name = "methodArgumentNotValidExceptionHandler")
	public MethodArgumentNotValidExceptionHandler methodArgumentNotValidExceptionHandler() {
		return new MethodArgumentNotValidExceptionHandler();
	}
	
	@Bean
	@ConditionalOnMissingBean(name = "methodArgumentTypeMismatchExceptionHandler")
	public MethodArgumentTypeMismatchExceptionHandler methodArgumentTypeMismatchExceptionHandler() {
		return new MethodArgumentTypeMismatchExceptionHandler();
	}
	
	@Bean
	@ConditionalOnMissingBean(name = "resourceNotFoundExceptionHandler")
	public ResourceNotFoundExceptionHandler resourceNotFoundExceptionHandler() {
		return new ResourceNotFoundExceptionHandler();
	}
	
	@Bean
	@ConditionalOnMissingBean(name = "unauthorizedExceptionHandler")
	public UnauthorizedExceptionHandler unauthorizedExceptionHandler() {
		return new UnauthorizedExceptionHandler();
	}
	
}
